package StudentAchievementManagementSystem.ui;

import StudentAchievementManagementSystem.component.AddStuGradeDialog;
import StudentAchievementManagementSystem.component.DeleteStuGradeDialog;
import StudentAchievementManagementSystem.component.UpdateStuGradeDialog;
import StudentAchievementManagementSystem.mysql.MysqlUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class GradeInsComponent extends Box {
    final int WIDTH=850;
    final int HEIGHT=600;

    JFrame jf = null;
    private JTable table;
    private Vector<String> titles;
    private Vector<Vector> tableData;
    private DefaultTableModel tableModel;

    public GradeInsComponent(JFrame jf) throws SQLException, IOException, ClassNotFoundException {
        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图
        this.jf = jf;
        JPanel btnPanel = new JPanel();
        Color color = new Color(203,220,217);
        btnPanel.setBackground(color);
        btnPanel.setMaximumSize(new Dimension(WIDTH,80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton addBtn = new JButton("添加");
        JButton updateBtn = new JButton("修改");
        JButton deleteBtn = new JButton("删除");
        JButton flushBtn = new JButton("刷新");


//        addBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                //弹出一个对话框，让用户输入图书的信息
//                new AddBookDialog(jf, "添加图书", true, new ActionDoneListener() {
//                    @Override
//                    public void done(Object result) {
//                        requestData();
//                    }
//                }).setVisible(true);
//            }
//        });
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddStuGradeDialog(jf,"添加学生信息",true).setVisible(true);
            }
        });
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteStuGradeDialog(jf,"删除学生信息",true).setVisible(true);
            }
        });
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {new UpdateStuGradeDialog(jf,"更新成绩信息",true).setVisible(true);}
        });
        flushBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    requestData();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
        });
//
//        updateBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                //获取当前表格选中的id
//                int selectedRow = table.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1
//
//                if (selectedRow==-1){
//                    JOptionPane.showMessageDialog(jf,"请选择要修改的条目！");
//                    return;
//                }
//
//                String id = tableModel.getValueAt(selectedRow, 0).toString();
//
//                //弹出一个对话框，让用户修改
//                new UpdateBookDialog(jf, "修改图书", true, new ActionDoneListener() {
//                    @Override
//                    public void done(Object result) {
//                        requestData();
//                    }
//                },id).setVisible(true);
//            }
//        });

//        deleteBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                //获取选中的条目
//                int selectedRow = table.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1
//
//                if (selectedRow==-1){
//                    JOptionPane.showMessageDialog(jf,"请选择要删除的条目！");
//                    return;
//                }
//
//                //防止误操作
//                int result = JOptionPane.showConfirmDialog(jf, "确认要删除选中的条目吗？", "确认删除", JOptionPane.YES_NO_OPTION);
//                if (result != JOptionPane.YES_OPTION){
//                    return;
//                }
//
//                String id = tableModel.getValueAt(selectedRow, 0).toString();
//
//                GetUtils.get("http://localhost:8080/book/deleteBookById?id=" + id, new SuccessListener() {
//                    @Override
//                    public void success(String result) {
//                        ResultInfo info = JsonUtils.parseResult(result);
//                        if (info.isFlag()){
//                            //删除成功
//                            JOptionPane.showMessageDialog(jf,"删除成功");
//                            requestData();
//                        }else{
//                            //删除失败
//                            JOptionPane.showMessageDialog(jf,info.getMessage());
//                        }
//
//                    }
//                }, new FailListener() {
//                    @Override
//                    public void fail() {
//                        JOptionPane.showMessageDialog(jf,"网络异常，请稍后重试");
//                    }
//                });
//
//            }
//        });

        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(flushBtn);

        this.add(btnPanel);

        //组装表格
        String[] ts = {"学号","课程号","姓名","专业","学科","分数"};
        titles = new Vector<>();
        for (String title : ts) {
            titles.add(title);
        }

        tableData = new Vector<>();

        tableModel = new DefaultTableModel(tableData,titles);
        table = new JTable(tableModel){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
//        //设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);

        requestData();
    }

    //请求数据
    public void requestData() throws SQLException, IOException, ClassNotFoundException {
        Connection connection = MysqlUtil.getConnection();
        Statement statement = connection.createStatement();
        String sql = "select student.sno,course.cno,sname,major,course.cname,grade from student ,studentcoursegrade,course where student.sno=studentcoursegrade.sno and course.cno=studentcoursegrade.cno ";
        Vector<Vector> vectors = new Vector<Vector>();
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()){
            Vector<String> vector1 = new Vector < String > ();
            for (int i=1;i<7;i++){
                vector1.add(resultSet.getString(i));
            }
            try{
                vectors.add(vector1);
//                vector1.clear();
            }catch (NullPointerException e){
                e.printStackTrace();
            }
        }
        //清空tableData的数据
        tableData.clear();
        for (Vector vector : vectors) {
            tableData.add(vector);
        }
        //刷新表格
        tableModel.fireTableDataChanged();
    }
//        GetUtils.get("http://localhost:8080/book/findAllBook", new SuccessListener() {
//            @Override
//            public void success(String result) {
//                ResultInfo info = JsonUtils.parseResult(result);
//                Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
//
//                //清空tableData的数据
//                tableData.clear();
//                for (Vector vector : vectors) {
//                    tableData.add(vector);
//                }
//
//                //刷新表格
//                tableModel.fireTableDataChanged();
//            }
//        }, new FailListener() {
//            @Override
//            public void fail() {
//            }
//        });
}
